<template>
  <div class="bar-chart-container">
    <div ref="barChart" style="width: 100%; height: 300px;"></div>
  </div>
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'LeaveBarChart',
  mounted() {
    this.initChart()
  },
  methods: {
    initChart() {
      const chart = echarts.init(this.$refs.barChart)

      const option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          },
          formatter: '{b}<br/>加班时长: {c}小时'
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          type: 'value',
          name: '加班时长(小时)',
          axisLine: {
            lineStyle: {
              color: '#999'
            }
          }
        },
        yAxis: {
          type: 'category',
          data: ['技术部', '市场部', '人事部', '财务部', '行政部'],
          axisLine: {
            lineStyle: {
              color: '#999'
            }
          }
        },
        series: [
          {
            name: '加班时长',
            type: 'bar',
            data: [320, 280, 150, 120, 80],
            itemStyle: {
              color: function(params) {
                const colorList = ['#5470C6', '#91CC75', '#FAC858', '#EE6666', '#73C0DE'];
                return colorList[params.dataIndex]
              },
              borderRadius: [0, 4, 4, 0]
            },
            label: {
              show: true,
              position: 'right',
              formatter: '{c}小时'
            }
          }
        ]
      }

      chart.setOption(option)

      window.addEventListener('resize', function() {
        chart.resize()
      })
    }
  }
}
</script>

<style scoped>
.bar-chart-container {
  width: 100%;
}
</style>